$(function () {
  //接收id
  // console.log(location.href.split("="));
  // console.log(location.href.split("=")[1]);
  function initForm() {
    let id = location.href.split("=")[1];
    //发送ajax
    axios({
      method: "GET",
      //删除 .  保留
      url: "/my/article/" + id,
    }).then((res) => {
      //判断
      if (res.data.status != 0) {
        return layer.msg(res.data.message);
      }
      console.log(res.data);
      //成功后赋值
      form.val("formEdit", res.data.data);
      setTimeout(function () {
        tinyMCE.activeEditor.setContent(res.data.data.content);
      }, 500);
      //封面赋值
      let baseURL = "http://api-breakingnews-web.itheima.net";
      let url = baseURL + res.data.data.cover_img;
      $image.cropper("destroy").attr("src", url).cropper(options);
    });
  }
  //需求1
  let layer = layui.layer;
  let form = layui.form;
  initArtCateList();
  function initArtCateList() {
    axios({
      method: "GET",
      url: "/my/article/cates",
    }).then((res) => {
      if (res.data.status != 0) {
        return layer.msg(res.data.message);
      }
      let arr = ['<option value="">请选择文章类别</option>'];
      res.data.data.forEach((ele) => {
        arr.push(`
        <option value="${ele.Id}">${ele.name}</option>
        `);
      });
      //赋值
      let str = arr.join("");
      $("[name=cate_id]").empty().html(str);
      form.render();
      //用于修改的form表单赋值
      initForm();
    });
  }
  //需求2:初始化富文本编辑器
  initEditor();
  //需求3 文章封面
  //1 初始化图片裁剪器
  var $image = $("#image");
  var options = {
    aspectRatio: 400 / 280,
    preview: ".img-preview",
  };
  $image.cropper(options);

  //需求四:确认状态
  let state;
  $("#btnSave1").on("click", function () {
    state = "已发布";
  });
  $("#btnSave2").on("click", function () {
    state = "草稿";
  });

  //需求五
  $("#chooseImage").on("click", function () {
    $("#file").click();
  });
  $("#file").on("change", function (e) {
    var file = e.target.files[0];
    if (file == undefined) {
      return layer.msg("此文章封面为空!");
    }
    var newImgURL = URL.createObjectURL(file);
    $image.cropper("destroy").attr("src", newImgURL).cropper(options);
  });
  //表单提交
  $("#formPub").on("submit", function (e) {
    e.preventDefault();
    let fd = new FormData(this);
    fd.append("state", state);
    $image
      .cropper("getCroppedCanvas", {
        width: 400,
        height: 200,
      })
      .toBlob(function (blob) {
        fd.append("cover_img", blob);
        console.log(...fd);
        //第一种 Jquery中的ajax - 两个false
        // let baseURL = "http://api-breakingnews-web.itheima.net";
        // $.ajax({
        //   method: "POST",
        //   url: baseURL + "/my/article/add",
        //   contentType: false,
        //   processData: false,
        //   data: fd,
        //   headers: {
        //     Authorization: localStorage.getItem("token"),
        //   },
        //   success: function (res) {
        //     console.log(res);
        //     layer.msg(res.message);
        //   },
        // });
        //第二种 axios 发布文章
        axios({
          method: "POST",
          url: "/my/article/edit",
          data: fd,
        }).then((res) => {
          if (res.data.status != 0) {
            return layer.msg(res.data.message);
          }
          layer.msg("恭喜您,修改文章成功!");
          //页面跳转
          setTimeout(
            window.parent.document.querySelector("#art_list").click(),
            1500
          );
        });
      });
  });
});
